package com.visionet.school.app.web.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hxpeng on 2017/3/22.
 */
public class CommonUtils {

    /*
	 * 检查是否微信浏览器
	 */

    public static boolean isWeixinBrower(HttpServletRequest request) {
        if (request == null) {
            return false;
        }
        if (StringUtils.isBlank(request.getHeader("User-Agent"))) {
            return false;
        }
        String agent = request.getHeader("User-Agent").toLowerCase();
        return agent.indexOf("micromessenger") > 0;
    }

    /**
     * 半角转全角
     *
     * @param input
     *            String.
     * @return 全角字符串.
     */
    public static String ToSBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                c[i] = '\u3000';
            } else if (c[i] < '\177') {
                c[i] = (char) (c[i] + 65248);

            }
        }
        return new String(c);
    }

    /**
     * 全角转半角
     *
     * @param input
     *            String.
     * @return 半角字符串
     */
    public static String ToDBC(String input) {

        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '\u3000') {
                c[i] = ' ';
            } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
                c[i] = (char) (c[i] - 65248);

            }
        }
        String returnString = new String(c);

        return returnString;
    }
    /**
     * 特殊字符 转换成全角字符
     *
     * @param keyword
     * @return
     */
    public static String escapeExprSpecialWord(String keyword) {
        if (StringUtils.isNotBlank(keyword)) {
            String[] fbsArr = { "@", "%","#","\\$", "(", ")", "\\*", "\\+", "\\.", "\\[", "\\]", "\\?", "\\^", "\\{", "\\}","<", ">", "&", "\\|" };
            for (String key : fbsArr) {
                if (keyword.contains(key)) {
                    keyword = keyword.replaceAll(key, ToSBC(key));
                }
            }
        }
        return keyword;
    }
}
